package com.xmkj.bearuser.sort;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;

import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.SecondSortBean;
import com.common.retrofit.bean.ThreeCategroyBean;
import com.common.retrofit.bearusermethod.GetThreeMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.sort.adapter.ThreeListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ThreeListActivity extends BaseMvpActivity {
    public static final String BEAN = "bean";
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    private SecondSortBean.CategoryTwoEntity categoryTwoEntity;
    private ThreeListAdapter homeReccommendListAdapter;
    List<ThreeCategroyBean>  homeReccommedBeans = new ArrayList<>();

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_reclerview;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        setRecommendRecyclerView();
        gotoRecommendListHttp();
    }

    @Override
    protected void setNavigation() {
        categoryTwoEntity = (SecondSortBean.CategoryTwoEntity) getIntent().getSerializableExtra(BEAN);
        setNavigationBack(categoryTwoEntity.c_name+"分类");
    }

    private void setRecommendRecyclerView() {

        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(true);
        recyclerview.setLoadMoreEnabled(true);
        homeReccommendListAdapter = new ThreeListAdapter(context, homeReccommedBeans);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(homeReccommendListAdapter);

        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoRecommendListHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoRecommendListHttp();
            }
        });

    }

    private void gotoRecommendListHttp() {
        CommonSubscriber<ArrayList<ThreeCategroyBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<ThreeCategroyBean> beans = (ArrayList<ThreeCategroyBean>) o;
                List<ThreeCategroyBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    homeReccommendListAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    homeReccommedBeans = beanList;
                    homeReccommendListAdapter.addAll(homeReccommedBeans);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                homeReccommendListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                homeReccommendListAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetThreeMethods.getInstance().goodsCategory(subscriber, SPUtils.getShareInt(Constans.UID)+"",SPUtils.getShareString(Constans.HASHID),categoryTwoEntity.c_id+"",mPageIndex);
        rxManager.add(subscriber);
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
